﻿using System.IO;
using System.Xml.Serialization;
using CSWriter.Blogs;
using System;
using CommunityServerRest;
using System.Reflection;

namespace CSWriter {
	public class Settings {

		public Settings() {

			AppPath = GetApplicationPath();
			DraftsPath = Path.Combine(AppPath, "Drafts");
			PostsPath = Path.Combine(AppPath, "Posts");

			PostExtension = ".xml";

			RecentPosts = new RecentPostList();
			ImageOptions = new ImageOptions();
			ImageOptions.Resample = false;
		}

		private static string GetApplicationPath() {
			string s = Assembly.GetCallingAssembly().GetName().CodeBase.ToString();
			s = Path.GetDirectoryName(s);
			s = s + "\\";
			return s;
		}

		public static Settings Load() {
			string path = Path.Combine(GetApplicationPath(), "Settings.xml");

			XmlSerializer serializer = new XmlSerializer(typeof(Settings));
			StreamReader reader;
			Settings settings;

			reader = new StreamReader(path);

			try {	
				settings = (Settings)serializer.Deserialize(reader);
			} catch (Exception ex) {
				throw ex;
			} finally {
				reader.Close();
			}

			return settings;
		}
		public void Save(string path) {
			XmlSerializer serializer = new XmlSerializer(typeof(Settings));
			StreamWriter writer = new StreamWriter(path);
			serializer.Serialize(writer, this);
			writer.Close();
		}
		public ImageOptions ImageOptions { get; set; }
		public RecentPostList RecentPosts { get; set; }
		public BlogInfo DefaultBlog { get; set; }
		public ServiceCredentials ServiceCredentials { get; set; }

		[System.Xml.Serialization.XmlIgnore]
		public static string DraftsPath { get; set; }
		[System.Xml.Serialization.XmlIgnore]
		public static string AppPath { get; set; }
		[System.Xml.Serialization.XmlIgnore]
		public static string PostsPath { get; set; }
		[System.Xml.Serialization.XmlIgnore]
		public static string PostExtension { get; set; }
		
	}
}
